<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/21
 * Time: 11:02
 */
namespace app\business\controller;

use think\Controller;
use think\captcha\Captcha;
use think\Db;

class Register extends Controller
{
	/**
	 * 注册首页
	*/
	public function index()
	{
		return view('index');
	}
	
	/**
	 * 验证码
	*/
	public function registercode()
	{
		$captcha = new Captcha();
		$captcha->fontSize = 30;
		$captcha->length = 4;
		$captcha->expire = 300;
		return $captcha->entry();
	}
	
	/**
	 * 邮箱验证
	 * @param string post.email 邮箱
	*/
	public function email()
	{
		if(request()->isAjax()){
			try{
				$email = input('post.email');
				$result = Db::table('business')->where('email',$email)->where('status',2)->find();
				if($result){
					throw new \PDOException("邮箱{$email}已存在");
				}
				$success = ['success'=>'可用'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 注册
	 * @param string post.email 邮箱
	 * @param string post.code 验证码
	 * @param string post.password 密码
	 * @param string post.pass确认密码
	 * @param string post.successively 付款方式
	*/
	public function reg()
	{
		if(request()->isAjax()){
			try{
				$code = input('post.code');
				if(!captcha_check($code)){
					throw new \PDOException('验证码错误');
				}
				$input['email'] = input('post.email');
				if(!$input['email']){
					throw new \PDOException('请输入邮箱');
				}
				if(!input('post.password')){
					throw new \PDOException('请输入密码');
				}
				$input['password'] = encrypt(input('post.pass'));
				if(!input('post.successively')){
					throw new \PDOException('请选择付款方式');
				}
				$input['successively'] = input('post.successively');
				$input['token'] = $this->token();
				$input['status'] = 3;
				$id = Db::table('business')->insertGetId($input);
				if(!$id){
					throw new \PDOException('注册失败');
				}
				$title = "新品有戏商家合作平台注册";
				$content = "亲爱的" . $input['email'] . "：<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/><a href='http://192.168.1.101/company/shoping/shop/public/business/register/doreg?token=" .$input['token']. "' target='_blank'>http://192.168.1.101/company/shoping/shop/public/business/register/doreg?token=" .$input['token']."</a><br/>如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问，该链接24小时内有效。<br/>如果此次激活请求非你本人所发，请忽略本邮件。<br/>";
				$result = sendMail($input['email'],$title,$content);
				if($result){
					//发送成功
					$success = ['success'=>"邮件发送成功，请注意查看邮箱"];
					return json($success);
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 注册成功/失败
	*/
	public function doreg()
	{
		if(input('get.token')){
			$result = Db::table('business')->where('token',trim($_GET['token']))->find();
			if($result){
				$res = Db::table('business')->where('token',$result['token'])->update(['status'=>2]);
				if($res){
					return view('success');
				}else{
					return view('fail');
				}
			}
		}else{
			return view('404');
		}
	}
	/**
	 * 生成token
	 * @return string $token
	 * */
	public function token($length = 10)
	{
		// 密码字符集，可任意添加你需要的字符
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$token = time();
		for ( $i = 0; $i < $length; $i++ )
		{
			$token .= $chars[ mt_rand(0, strlen($chars) - 1) ];
		}
		return $token;
	}
	
	/**
	 * @param string post.email 邮箱
	 * @param string post.code 验证码
	 * @param string post.password 密码
	 * @param string post.pass确认密码
	 * @param string post.successively 付款方式
	*/
	public function resend()
	{
		if(request()->isAjax()){
			try{
				$code = input('post.code');
				if(!captcha_check($code)){
					throw new \PDOException('验证码错误');
				}
				$email = input('post.email');
				if(!$email){
					throw new \PDOException('请输入邮箱');
				}
				if(!input('post.password')){
					throw new \PDOException('请输入密码');
				}
				$input['password'] = encrypt(input('post.pass'));
				if(!input('post.successively')){
					throw new \PDOException('请选择付款方式');
				}
				$input['successively'] = input('post.successively');
				$input['token'] = $this->token();
				$input['status'] = 3;
				$info = Db::table('business')->where('email',$email)->find();
				$id = Db::table('business')->where('id',$info['id'])->update($input);
				if(!$id){
					throw new \PDOException('注册失败');
				}
				$title = "新品有戏商家合作平台注册";
				$content = "亲爱的" . $email . "：<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/><a href='http://192.168.1.103/company/shoping/shop/public/business/register/doreg?token=" .$input['token']. "' target='_blank'>http://192.168.1.103/company/shoping/shop/public/business/register/doreg?token=" .$input['token']."</a><br/>如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问，该链接24小时内有效。<br/>如果此次激活请求非你本人所发，请忽略本邮件。<br/>";
				$result = sendMail($email,$title,$content);
				if($result){
					//发送成功
					$success = ['success'=>"邮件发送成功，请注意查看邮箱"];
					return json($success);
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}